package com.etime.shyzuul.common;

import com.etime.shyzuul.pojo.SysRoleRight;
import com.etime.shyzuul.services.interfaces.SysRoleRightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据接口权限控制配置类
 * 在此类中初始化权限数据到缓存中
 */
@Configuration
public class RightConfig {
    @Autowired
    private SysRoleRightService sysRoleRightService;

    /**
     * 初始化权限信息
     * @return
     */
    @Bean
    public RightData initRightData(){
        List<SysRoleRight> list = sysRoleRightService.selectRoleRightList();
        Map<String,List<Integer>> rightMap = new HashMap<>();
        for(SysRoleRight right:list){
            //判断url在map中是否存在
            if(!rightMap.containsKey(right.getFunurl())){
                List<Integer> rolelist = new ArrayList<>();
                for(SysRoleRight r : list){
                    if(r.getFunurl().equals(right.getFunurl())){
                        rolelist.add(r.getRoleid());
                    }
                }
                if(rolelist.size()>0){
                    rightMap.put(right.getFunurl(),rolelist);
                }
            }
        }
        RightData rightData = new RightData();
        rightData.setRightmap(rightMap);
        return rightData;
    }
}
